<?php

namespace app\common\model\Product;

use think\Model;

// 引用软删除
use traits\model\SoftDelete;

class Order extends Model
{
    // 订单表
    protected $table = 'pre_order';

    // 自动写入时间
    protected $autoWriteTimestamp = true;

    // 创建时间 => 注册时间
    protected $createTime = 'createtime';

    // 更新时间
    protected $updateTime = false;
	
	// 软删除
	use SoftDelete;
	protected $deleteTime = 'deletetime';

    protected $append = [
        'status_text', //多张
		'region_text',
		'expid_text',
		'proimg_cdn', // 多图
		'order_cdn'
    ];
	
	// 多个商品图
	public function getOrderCdnAttr($value,$data)
	{
		$OrderProductList = model('Product.OrderProduct')->where('orderid',$data['id'])->select();
		
		$list = [];
		
		foreach($OrderProductList as $key => $item)
		{
			$Product = model('Product.Product')->where('id',$item['proid'])->find();
			
			$list[] = [
				$key => $Product
			];
		}
		
		return $list;
		
	}
	
	public function getProimgCdnAttr($value,$data)
	{
		// $cdn = config('cdn');
	
		$cdn = model('Config')->where(['key' => 'SiteUrl'])->value('value');
	
		$proimg = empty($data['proimg']) ? [] : trim($data['proimg']);
	
		if(empty($proimg))
		{
			// return '暂无评价图片';
			// return $proimg[] = [
			// 	'暂无评论图片'
			// ];
			return $proimg;
		}
	
		//字符串替换
		$proimg = str_replace("uploads/", $cdn."/uploads/", $proimg);
	
		//在转换为数组
		// $list = explode(',', $thumbs);
		$list = json_decode($proimg,true);
	
		if(count($list) > 0)
		{
			return $list;
		}
	
	}
	
	// 关联用户表
	public function user()
	{
	    return $this->belongsTo('app\common\model\User\User','userid','id',[])->setEagerlyType(0);
	}
	
	// 关联地址表
	public function addres()
	{
	    return $this->belongsTo('app\common\model\User\Address','addrid','id',[])->setEagerlyType(0);
	}
	
	// 地区字段
	public function getRegionTextAttr($value,$data)
	{
	    $Address = model('User.Address')->where('id',$data['addrid'])->find();
		
		$Region = model('Region')->where('code',$Address['district'])->find();
		
		$RegionText = $Region['province'] . $Region['city'] . $Region['district'] . $Address['address'];
		
		return $RegionText;
		
	}
	
	protected function getExpidTextAttr($value, $data)
	{
		$expid = $data['expid'];
		
		$ExpidText = model('Express')->where('id',$expid)->value('name');
		
		return $ExpidText;
	}

    protected function getStatusTextAttr($value, $data)
    {
        $status = $data['status'];
        $text = '';

        switch($status)
        {
            case 1:
                $text = '已支付';
                break;
            case 2:
                $text = '已发货';
                break;
            case 3:
                $text = '已收货';
                break;
            case 4:
                $text = '已评价';
                break;
            case -1:
                $text = '已退货';
                break;
            case -2:
                $text = '退货中';
                break;
            case -3:
                $text = '拒绝退货';
                break;
            default:
                $text= '未知状态';
        }

        return $text;
    }
}
